package cn.wawi.controller.sys;

import java.util.List;
import javax.annotation.Resource;
import cn.wawi.common.R;
import cn.wawi.controller.BaseController;
import cn.wawi.dao.sys.RoleDao;
import cn.wawi.entity.sys.Role;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * controller继承 BaseController 拥有
 * findAll       查询所有不分页
 * findOne       查询一条记录
 * findAllByPage 查询所有，分页
 * deleteBatch 批量删除
 * updateBatch 批量更新状态
 * deleteOne 根据主键删除
 * saveORupdate 主键id不为空时更新,否则保存
 */
@Controller
@RequestMapping("/sys_role")
public class RoleController extends BaseController<Role>{
	@Resource
	protected RoleDao<Role> roleDao;
	
	@ResponseBody
	@RequestMapping(value="/getUserRole",method={RequestMethod.GET,RequestMethod.POST})
	public R<Role> getUserRole(@RequestParam(value="userId",required=true)int userId){
		List<Role> list=roleDao.getUserRole(userId);
		return success("请求成功!").put(list, list.size());
	}

	@ResponseBody
	@RequestMapping(value="/saveUserRole",method={RequestMethod.POST})
	@Transactional(rollbackFor=Exception.class)
	public R<Role> saveUserRole(@RequestParam(value="userId",required=true)Integer userId,@RequestParam(value="roleIds[]")List<Integer> roleIds){
		roleDao.deleteUserRole(userId);
		for (int roleId: roleIds) {
			roleDao.saveUserRole(userId,roleId);	
		}
		return success("保存用户角色成功!");
	}
	@ResponseBody
	@RequestMapping(value="/deleteUserRole",method={RequestMethod.POST})
	public R<Role> deleteUserRole(@RequestParam(value="userId",required=true)Integer userId){
		roleDao.deleteUserRole(userId);
		return success("删除用户角色成功!");
	}
}
